﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XiTi
{
    public partial class WhiteBoardControl : UserControl
    {
        public WhiteBoardControl()
        {
            InitializeComponent();

        }

        public void setImg(Image bmp)
        {
            bg_image = bmp;
            pictureBox1.Image = this.bg_image;

            //bg_image = new Bitmap(bg_image.Width, bg_image.Height);
            //中间画布
            tmp_bitmap = new Bitmap(bg_image.Width, bg_image.Height);
            //创建空白画板
            draw_paint = Graphics.FromImage(tmp_bitmap);

            linePoints = new PointF[1000];
            for (int i = 0; i < linePoints.Length; i++)
            {
                linePoints[i] = new PointF();
            }
            pointNum = 0;
            bt_paint.Checked = true;

            pictureBox1.Invalidate();
        }

        public event EventHandler Close;

        int line_width = 3;
        Pen line_color = new Pen(Color.Red, 3);
        SolidBrush eraser_brush = new SolidBrush(Color.Transparent);

        private Graphics draw_paint, merge_paint;//中间画板
        private Image tmp_bitmap;//用来保存绘图痕迹

        PointF[] linePoints;//存储绘制多边形的点集
        int pointNum = 0;

        Image bg_image;//背景图片
        Point end;   //结束点

        string action_type = "画笔";//绘图动作类型
        bool press_move_flag = true;

        private void bt_paint_Click(object sender, EventArgs e)
        {
            action_type = "画笔";
            line_width = 3;

            bt_paint.Checked = true;
            bt_rubber.Checked = false;
        }

        private void bt_rubber_Click(object sender, EventArgs e)
        {
            action_type = "橡皮擦";
            line_width = 30;

            bt_paint.Checked = false;
            bt_rubber.Checked = true;
        }


        private Bitmap get_merge_bitmap()
        {
            Bitmap tmp_bmp = (Bitmap)bg_image.Clone();
            merge_paint = Graphics.FromImage(tmp_bmp);
            merge_paint.DrawImage(tmp_bitmap, 0, 0);//将中间画布绘制的内容绘制到原始画布上
            merge_paint.Dispose();
            return tmp_bmp;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (action_type.IndexOf("画笔") >= 0)
            {
                if (e.Button == MouseButtons.Left)
                {
                    press_move_flag = true;
                    pointNum = 0;
                    linePoints[pointNum].X = e.X - line_width / 2;
                    linePoints[pointNum].Y = e.Y - line_width / 2;
                    pointNum++;
                }
            }
            else if (action_type.IndexOf("橡皮擦") >= 0)
            {
                if (e.Button == MouseButtons.Left)
                {
                    press_move_flag = true;
                    //模式转换为'CompositingMode.SourceCopy'
                    draw_paint.CompositingMode = CompositingMode.SourceCopy;
                    //用透明色替换指定的区域
                    draw_paint.FillEllipse(eraser_brush, e.X - line_width / 2, e.Y - line_width / 2, line_width, line_width);
                    //模式转换为'CompositingMode.SourceOver',这样后面可以进行绘图
                    //draw_paint.CompositingMode = CompositingMode.SourceOver;
                }
            }
            end = e.Location;
            pictureBox1.Invalidate();
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (action_type.IndexOf("画笔") >= 0)
            {
                if (press_move_flag == true && e.Button == MouseButtons.Left)
                {
                    end = e.Location;

                    linePoints[pointNum].X = e.X - line_width / 2;
                    linePoints[pointNum].Y = e.Y - line_width / 2;
                    pointNum++;

                    //pictureBox1.Image = get_merge_bitmap();
                    pictureBox1.Invalidate();//此代码不可省略
                }
            }
            else if (action_type.IndexOf("橡皮擦") >= 0)
            {
                if (press_move_flag == true && e.Button == MouseButtons.Left)
                {
                    end = e.Location;
                    //用透明色替换指定的区域
                    draw_paint.FillEllipse(eraser_brush, e.X - line_width / 2, e.Y - line_width / 2, line_width, line_width);
                    pictureBox1.Image = get_merge_bitmap();
                    pictureBox1.Invalidate();//此代码不可省略
                    Refresh();
                }
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (action_type.IndexOf("画笔") >= 0)
            {
                press_move_flag = false;
                if (pointNum > 2)
                {
                    draw_paint.SmoothingMode = SmoothingMode.AntiAlias;
                    draw_paint.DrawCurve(line_color, linePoints, 0, pointNum - 2);
                    //e.Graphics.FillEllipse(Brushes.Blue, end.X - (line_width / 2), end.Y - (line_width / 2), line_width, line_width);
                }
                pictureBox1.Image = get_merge_bitmap();
                pictureBox1.Invalidate();//此代码不可省略
            }
            else if (action_type.IndexOf("橡皮擦") >= 0)
            {
                //模式转换为'CompositingMode.SourceOver',这样后面可以进行绘图
                draw_paint.CompositingMode = CompositingMode.SourceOver;
                press_move_flag = false;
                pictureBox1.Invalidate();//此代码不可省略
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            PictureBox pic = sender as PictureBox;
            if (action_type.IndexOf("画笔") >= 00)
            {
                if (press_move_flag)
                {
                    if (pointNum > 2)
                    {
                        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                        e.Graphics.DrawCurve(line_color, linePoints, 0, pointNum - 2);
                        //e.Graphics.FillEllipse(Brushes.Blue, end.X - (line_width / 2), end.Y - (line_width / 2), line_width, line_width);
                    }
                }
            }
            else if (action_type.IndexOf("橡皮擦") >= 0)
            {
                if (press_move_flag)
                {
                    e.Graphics.FillEllipse(Brushes.Blue, end.X - (line_width / 2), end.Y - (line_width / 2), line_width, line_width);
                }
            }
        }
        private void bt_close_Click(object sender, EventArgs e)
        {
            if (Close != null)
            {
                Close(sender, e);
            }
        }
    }
}
